//导入城市数据
var city = require('../../../utils/city.js');
var city1 = require('../../../utils/city1.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 轮播数据
    swiper: {
      indicatorDots: true,
      autoplay: true,
      circular: true,
      interval: 3000,
      duration: 500,
      color:"rgba(0,0,0,.3)",
      acolor:"#000",
      imgUrls: [{
        img: "../resource/images/swiper_item.png",
        id: 0
      }, {
        img: "../resource/images/swiper_item.png",
        id: 1
      }]
    },
    // 导航
    nav: {
      // nav_title
      nav_list: ["门诊", "脑外科", "妇科", "妇产科", "血液科", "皮肤科"],
      currentTab: 0
    },
    // 底部数据
    footer: {
      footdex: 0,
      txtcolor: "#A2A2A2",
      seltxt: "#EC6464",
      background: "#fff",
      list: [
        {
          url: "/pages/index/index",
          icons: "/pages/resource/images/f_1.png",
          selIcon: "/pages/resource/images/f_1_sel.png",
          text: "探一探"
        }, {
          url: "/pages/mine/mine",
          icons: "/pages/resource/images/f_2.png",
          selIcon: "/pages/resource/images/f_2_sel.png",
          text: "个人"
        }
      ]
    },
    // 选择城市
    citys: {
      currentCity: '定位中...',
      letterArr: [],
      city1: [],
      hotCitys: [{ id: "105", provincecode: "340000", city: "安庆市", code: "340800", initial: "A" },
      { id: "7", provincecode: "130000", city: "保定市", code: "130600", initial: "B" }]
    }
  },
  // 切换nav
  swichNav: function (e) {
    var nav = this.data.nav;
    nav.currentTab = e.currentTarget.dataset.current;
    this.setData({
      nav: nav
    })
  },
  //三级联动默认选择
  choose: function (a, b, c, array) { //第一个值，第二个值，第三个值，数组
    var that = this
    var multiArray = that.data.multiArray
    var multiIndex = that.data.multiIndex
    var arr = []
    for (var i in array) {
      arr.push(array[i].name)
      var arr1 = []
      for (var j in array[a].children) {
        arr1.push(array[a].children[j].name)
        var arr2 = []
        for (var k in array[a].children[b].children) {
          arr2.push(array[a].children[b].children[k].name)
        }
      }
    }
    multiArray.push(arr) //第一个数组内容
    multiArray.push(arr1)
    multiArray.push(arr2)
    multiIndex.push(a)
    multiIndex.push(b)
    multiIndex.push(c)
    that.setData({
      multiArray: multiArray,
      multiIndex: multiIndex,
    })



    // 列表
    recommendArr: [
      {
        img: "../images/active_05.jpg",
        title: "洪市西甲先水饺（桥西店）",
        value: "2321",
        prompt: "你常去的老店，正在召唤你",
        quanArr: [
          { img: "../images/quan.png", con: "首单20元优惠券", image: "../images/youhui.png" },
          { img: "../images/kan.png", con: "邀请好友帮砍价享免单" }
        ]
      },
      {
        img: "../images/active_05.jpg",
        title: "洪市西甲先水饺（桥西店）",
        value: "2321",
        prompt: "你常去的老店，正在召唤你",
        quanArr: [
          { img: "../images/quan.png", con: "首单20元优惠券", image: "../images/youhui.png" },
          { img: "../images/tuan.png", con: "双人餐49.9元起，4人餐88元起" }
        ]
      },
      {
        img: "../images/active_05.jpg", title: "洪市西甲先水饺（桥西店）",
        value: "2321",
        prompt: "你常去的老店，正在召唤你",
        quanArr: [
          { img: "../images/qiang.png", con: "原价88元双人餐，现价28.8元秒杀" }
        ]
      }
    ]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    if (options.index != undefined) {
      var footer = this.data.footer;
      footer.footdex = options.index;
      this.setData({
        footer: footer
      })




      //三级联动默认选择
      var citys = city.citys()
      that.choose(0, 1, 2, citys)

    };
    //选择城市
    var searchLetter = city.searchLetter;
    var cityList = city.cityList();
    var city1 = cityList
    var letterArr = searchLetter
    var citys = that.data.citys
    citys.city1 = city1
    citys.letterArr = letterArr
    that.setData({
      citys: citys
    })
  },



  //选择城市
  confirmClick: function (e) {
    var a_id = e.target.dataset.a_id
    var reg = new RegExp('[\u4e00-\u9fa5]+', 'g')
    if (!a_id.search(reg)) {
      if (a_id === "定位中..." || a_id === "获取定位失败") {

      } else {
        wx.reLaunch({
          url: '/hyb_jianzhi/index/index?a_id=' + a_id,
        })
      }
    }
  },
  //锚点跳转
  anchorClick: function (e) {
    var that = this
    var citys = that.data.citys
    var a_id = e.currentTarget.dataset.a_id
    citys.toView = a_id
    that.setData({
      citys: citys
    })
  },
  //获取所在市
  getLocation: function () {
    var that = this
    wx.getLocation({
      type: 'wgs84',   //<span class="comment" style="margin:0px;padding:0px;border:none;">默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标</span><span style="margin:0px;padding:0px;border:none;"> </span>  
      success: function (res) {
        // success    
        var longitude = res.longitude
        var latitude = res.latitude
        // console.log(longitude, latitude)
        that.loadCity(longitude, latitude)
      }
    })
  },
  loadCity: function (longitude, latitude) {
    var that = this
    wx.request({
      url: 'https://api.map.baidu.com/geocoder/v2/?ak=RU9iE3025rdZ1lzrxEYWje7DyInsLD2L&location=' + latitude + ',' + longitude + '&output=json',
      data: {},
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        // success    
        // console.log(res);
        var city = res.data.result.addressComponent.city;
        that.setData({ currentCity: city });
      },
      fail: function () {
        that.setData({ currentCity: "获取定位失败" });
      },
    })
  },



  //三级联动
  bindMultiPickerColumnChange: function (e) {
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    var citys = city.citys()
    var arr1 = []
    var arr2 = []
    for (let i = 0; i < citys.length; i++) {
      var arr = []
      if (citys[i].children == undefined) {
        arr.push()
        arr2.push()
      } else {
        var arr3 = []
        for (let j = 0; j < citys[i].children.length; j++) {
          arr.push(citys[i].children[j].name) //第二个数组内容
          if (citys[i].children == undefined) {
            arr3.push()
          } else if (citys[i].children[j].children == undefined) {
            arr3.push()
          } else {
            var arr4 = []
            for (let k = 0; k < citys[i].children[j].children.length; k++) {
              arr4.push(citys[i].children[j].children[k].name) //第三个数组内容
            }
          }
          arr3.push(arr4)
        }
        arr2.push(arr3)
      }
      arr1.push(arr)
    }
    for (let i = 0; i < citys.length; i++) {
      switch (e.detail.column) {
        case 0:
          if (citys[i].children == undefined) {
            switch (data.multiIndex[0]) {
              case i:
                data.multiArray[1] = []
                data.multiArray[2] = []
                break;
            }
            data.multiIndex[1] = 0;
            data.multiIndex[2] = 0;
            break;
          } else {
            for (let j = 0; j < citys[i].children.length; j++) {
              switch (data.multiIndex[0]) {
                case i:
                  data.multiArray[1] = arr1[i]
                  data.multiArray[2] = arr2[i][j]
                  break;
              }
              data.multiIndex[1] = 0;
              data.multiIndex[2] = 0;
              break;
            }
          }
        case 1:
          if (citys[i].children == undefined) {
            switch (data.multiIndex[0]) {
              case i:
                switch (data.multiIndex[1]) {
                  case i:
                    data.multiArray[2] = []
                    break;
                }
                break;
            }
          } else {
            for (let j = 0; j < citys[i].children.length; j++) {
              switch (data.multiIndex[0]) {
                case i:

                  switch (data.multiIndex[1]) {
                    case j:
                      data.multiArray[2] = arr2[i][j]
                      break;
                  }
                  break;
              }
            }
          }
          data.multiIndex[2] = 0;
          break;
      }
    }
    this.setData(data);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})